//
//  FileStorage.m
//  KaiLuShiSmallShop
//
//  Created by hunuo on 14-9-16.
//  Copyright (c) 2014年 hunuo. All rights reserved.
//

#import "FileStorage.h"


static NSFileManager *fm = nil;

@implementation FileStorage
+(NSFileManager *)sharedFileManager
{
    if(fm == nil)
    {
        fm = [NSFileManager defaultManager];
    }
    return fm;
}

+(NSString *)filePath:(NSString *)path
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *pa = [docPath stringByAppendingPathComponent:path];
    NSLog(@"----pa = %@",pa);
    return pa;
}
+(BOOL)isExistFilePath:(NSString *)path
{
    fm = [self sharedFileManager];
    
    if([fm fileExistsAtPath:[self filePath:path]])
    {
        return YES;
    }
    return NO;
}
+(void)createFilePath:(NSString *)path
{
    fm = [self sharedFileManager];
    
    if(![fm fileExistsAtPath:[self filePath:path]])
    {
        [fm createFileAtPath:[self filePath:path] contents:nil attributes:nil];
    }
}

+(void)writeFileArray:(NSArray *)arr inPath:(NSString *)path
{
    fm = [self sharedFileManager];
    
    if([fm fileExistsAtPath:[self filePath:path]])
    {
        [arr writeToFile:[self filePath:path] atomically:YES];
    }
}
+(NSArray *)getFileArrayFromPath:(NSString *)path
{
    fm = [self sharedFileManager];
    
    if([fm fileExistsAtPath:[self filePath:path]])
    {
        NSArray *arr = [NSArray arrayWithContentsOfFile:[self filePath:path]];
        NSLog(@"arr = %@",arr);
        return arr;
    }
    return nil;
}
+(void)deleteFileArrayWithPath:(NSString *)path
{
    fm = [self sharedFileManager];
    
    if([fm fileExistsAtPath:[self filePath:path]])
    {
        NSError *error;
        [fm removeItemAtPath:[self filePath:path] error:&error];
    }
}
+(void)updateFileArrayWithArray:(NSArray *)arr inPath:(NSString *)path
{
    fm = [self sharedFileManager];
    
    if([fm fileExistsAtPath:[self filePath:path]])
    {
        NSMutableArray *ar = [NSMutableArray arrayWithContentsOfFile:[self filePath:path]];
        [ar removeAllObjects];
        ar = [NSMutableArray arrayWithArray:arr];
        [ar writeToFile:[self filePath:path] atomically:YES];
    }
}
+(void)updatefileArrayWithObject:(NSString *)obj atIndex:(NSInteger)ind inPath:(NSString *)path
{
    fm = [self sharedFileManager];
    
    if([fm fileExistsAtPath:[self filePath:path]])
    {
         NSMutableArray *ar = [NSMutableArray arrayWithContentsOfFile:[self filePath:path]];
        [ar replaceObjectAtIndex:ind withObject:obj];
        [ar writeToFile:[self filePath:path] atomically:YES];
    }
}

#pragma marke NSUserDefault
+(void)NSUserDefault_setObject:(NSString *)obj forKey:(NSString *)key
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:obj forKey:key];
    [defaults synchronize];
}
+(NSString *)NSUserDefault_getObjectForKey:(NSString *)key
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *s = [defaults objectForKey:key];
    [defaults synchronize];
    return s;
}

+(void)NSUserDefault_setDic:(NSDictionary *)dic
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *keyArray = [dic allKeys];
    NSArray *valueArray = [dic allValues];
    for( int i=0;i<keyArray.count;i++)
    {
        [defaults setObject:[valueArray objectAtIndex:i] forKey:[keyArray objectAtIndex:i]];
    }
    [defaults synchronize];
}
/**
 NSUserDefault添加Obj
 */
+(void)NSUserDefault_setObj:(id)obj forKey:(NSString *)key
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:obj forKey:key];
    [defaults synchronize];
}

@end


